{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "848545bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b36c1865",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4148, 114)\n"
     ]
    }
   ],
   "source": [
    "Data = pd.read_csv('..\\Data_V2.csv', low_memory=False)\n",
    "print(Data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4487094",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data_Defect: (65, 114)\n"
     ]
    }
   ],
   "source": [
    "# 创建一个条件，判断缺陷情况等于0或1\n",
    "condition_def = Data['缺陷情况'] == 1\n",
    "Data_Defect = Data[condition_def] # 使用条件进行划分\n",
    "\n",
    "print('Data_Defect:', Data_Defect.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fcee1bc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>熔炼号</th>\n",
       "      <th>C</th>\n",
       "      <th>Si</th>\n",
       "      <th>Mn</th>\n",
       "      <th>P</th>\n",
       "      <th>S</th>\n",
       "      <th>Cu</th>\n",
       "      <th>Ni</th>\n",
       "      <th>Cr</th>\n",
       "      <th>Mo</th>\n",
       "      <th>...</th>\n",
       "      <th>一冷水波动频次8</th>\n",
       "      <th>一冷水波动频次9</th>\n",
       "      <th>一冷水波动频次10</th>\n",
       "      <th>西侧中包波动率1</th>\n",
       "      <th>西侧中包波动率2</th>\n",
       "      <th>东侧中包波动率1</th>\n",
       "      <th>东侧中包波动率2</th>\n",
       "      <th>角部裂纹实际值</th>\n",
       "      <th>缺陷情况</th>\n",
       "      <th>检测情况</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>107</th>\n",
       "      <td>23BC00155</td>\n",
       "      <td>0.21512</td>\n",
       "      <td>0.32351</td>\n",
       "      <td>1.30474</td>\n",
       "      <td>0.03667</td>\n",
       "      <td>0.02578</td>\n",
       "      <td>0.02386</td>\n",
       "      <td>0.01045</td>\n",
       "      <td>0.04097</td>\n",
       "      <td>0.00050</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>327</th>\n",
       "      <td>23BA00633</td>\n",
       "      <td>0.19362</td>\n",
       "      <td>0.18497</td>\n",
       "      <td>0.37758</td>\n",
       "      <td>0.02090</td>\n",
       "      <td>0.01687</td>\n",
       "      <td>0.03604</td>\n",
       "      <td>0.01355</td>\n",
       "      <td>0.03717</td>\n",
       "      <td>0.00449</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>895</th>\n",
       "      <td>23BB01045</td>\n",
       "      <td>0.21767</td>\n",
       "      <td>0.28462</td>\n",
       "      <td>1.40546</td>\n",
       "      <td>0.02226</td>\n",
       "      <td>0.02396</td>\n",
       "      <td>0.01773</td>\n",
       "      <td>0.01041</td>\n",
       "      <td>0.03788</td>\n",
       "      <td>0.01061</td>\n",
       "      <td>...</td>\n",
       "      <td>16.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>25.37</td>\n",
       "      <td>37.31</td>\n",
       "      <td>35.82</td>\n",
       "      <td>44.78</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>929</th>\n",
       "      <td>23BC00846</td>\n",
       "      <td>0.24865</td>\n",
       "      <td>0.27128</td>\n",
       "      <td>1.38830</td>\n",
       "      <td>0.02776</td>\n",
       "      <td>0.02778</td>\n",
       "      <td>0.01912</td>\n",
       "      <td>0.00874</td>\n",
       "      <td>0.03987</td>\n",
       "      <td>0.00337</td>\n",
       "      <td>...</td>\n",
       "      <td>22.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.75</td>\n",
       "      <td>13.75</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 114 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           熔炼号        C       Si       Mn        P        S       Cu       Ni  \\\n",
       "107  23BC00155  0.21512  0.32351  1.30474  0.03667  0.02578  0.02386  0.01045   \n",
       "327  23BA00633  0.19362  0.18497  0.37758  0.02090  0.01687  0.03604  0.01355   \n",
       "895  23BB01045  0.21767  0.28462  1.40546  0.02226  0.02396  0.01773  0.01041   \n",
       "929  23BC00846  0.24865  0.27128  1.38830  0.02776  0.02778  0.01912  0.00874   \n",
       "\n",
       "          Cr       Mo  ...  一冷水波动频次8  一冷水波动频次9  一冷水波动频次10  西侧中包波动率1  西侧中包波动率2  \\\n",
       "107  0.04097  0.00050  ...       NaN       NaN        NaN       NaN       NaN   \n",
       "327  0.03717  0.00449  ...       NaN       NaN        NaN       NaN       NaN   \n",
       "895  0.03788  0.01061  ...      16.0      11.0       15.0     25.37     37.31   \n",
       "929  0.03987  0.00337  ...      22.0      16.0        0.0      3.75     13.75   \n",
       "\n",
       "     东侧中包波动率1  东侧中包波动率2  角部裂纹实际值  缺陷情况  检测情况  \n",
       "107       NaN       NaN      2.0   1.0   1.0  \n",
       "327       NaN       NaN      1.0   1.0   1.0  \n",
       "895     35.82     44.78      1.0   1.0   1.0  \n",
       "929      0.00      0.00      1.0   1.0   1.0  \n",
       "\n",
       "[4 rows x 114 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Data_Defect.iloc[0:4, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c822ce67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data_Defect_known: (27, 114)\n",
      "Data_Defect_unknown: (38, 114)\n"
     ]
    }
   ],
   "source": [
    "# 创建一个条件，判断缺陷情况\n",
    "condition_known = Data_Defect['角部裂纹实际值'] != 42\n",
    "condition_unknown = Data_Defect['角部裂纹实际值'] == 42\n",
    "Data_Defect_known = Data_Defect[condition_known]\n",
    "Data_Defect_unknown = Data_Defect[condition_unknown]\n",
    "print('Data_Defect_known:', Data_Defect_known.shape)\n",
    "print('Data_Defect_unknown:', Data_Defect_unknown.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f5b276d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>熔炼号</th>\n",
       "      <th>C</th>\n",
       "      <th>Si</th>\n",
       "      <th>Mn</th>\n",
       "      <th>P</th>\n",
       "      <th>S</th>\n",
       "      <th>Cu</th>\n",
       "      <th>Ni</th>\n",
       "      <th>Cr</th>\n",
       "      <th>Mo</th>\n",
       "      <th>...</th>\n",
       "      <th>一冷水波动频次8</th>\n",
       "      <th>一冷水波动频次9</th>\n",
       "      <th>一冷水波动频次10</th>\n",
       "      <th>西侧中包波动率1</th>\n",
       "      <th>西侧中包波动率2</th>\n",
       "      <th>东侧中包波动率1</th>\n",
       "      <th>东侧中包波动率2</th>\n",
       "      <th>角部裂纹实际值</th>\n",
       "      <th>缺陷情况</th>\n",
       "      <th>检测情况</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>107</th>\n",
       "      <td>23BC00155</td>\n",
       "      <td>0.21512</td>\n",
       "      <td>0.32351</td>\n",
       "      <td>1.30474</td>\n",
       "      <td>0.03667</td>\n",
       "      <td>0.02578</td>\n",
       "      <td>0.02386</td>\n",
       "      <td>0.01045</td>\n",
       "      <td>0.04097</td>\n",
       "      <td>0.00050</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>327</th>\n",
       "      <td>23BA00633</td>\n",
       "      <td>0.19362</td>\n",
       "      <td>0.18497</td>\n",
       "      <td>0.37758</td>\n",
       "      <td>0.02090</td>\n",
       "      <td>0.01687</td>\n",
       "      <td>0.03604</td>\n",
       "      <td>0.01355</td>\n",
       "      <td>0.03717</td>\n",
       "      <td>0.00449</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>895</th>\n",
       "      <td>23BB01045</td>\n",
       "      <td>0.21767</td>\n",
       "      <td>0.28462</td>\n",
       "      <td>1.40546</td>\n",
       "      <td>0.02226</td>\n",
       "      <td>0.02396</td>\n",
       "      <td>0.01773</td>\n",
       "      <td>0.01041</td>\n",
       "      <td>0.03788</td>\n",
       "      <td>0.01061</td>\n",
       "      <td>...</td>\n",
       "      <td>16.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>25.37</td>\n",
       "      <td>37.31</td>\n",
       "      <td>35.82</td>\n",
       "      <td>44.78</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>929</th>\n",
       "      <td>23BC00846</td>\n",
       "      <td>0.24865</td>\n",
       "      <td>0.27128</td>\n",
       "      <td>1.38830</td>\n",
       "      <td>0.02776</td>\n",
       "      <td>0.02778</td>\n",
       "      <td>0.01912</td>\n",
       "      <td>0.00874</td>\n",
       "      <td>0.03987</td>\n",
       "      <td>0.00337</td>\n",
       "      <td>...</td>\n",
       "      <td>22.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.75</td>\n",
       "      <td>13.75</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 114 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           熔炼号        C       Si       Mn        P        S       Cu       Ni  \\\n",
       "107  23BC00155  0.21512  0.32351  1.30474  0.03667  0.02578  0.02386  0.01045   \n",
       "327  23BA00633  0.19362  0.18497  0.37758  0.02090  0.01687  0.03604  0.01355   \n",
       "895  23BB01045  0.21767  0.28462  1.40546  0.02226  0.02396  0.01773  0.01041   \n",
       "929  23BC00846  0.24865  0.27128  1.38830  0.02776  0.02778  0.01912  0.00874   \n",
       "\n",
       "          Cr       Mo  ...  一冷水波动频次8  一冷水波动频次9  一冷水波动频次10  西侧中包波动率1  西侧中包波动率2  \\\n",
       "107  0.04097  0.00050  ...       NaN       NaN        NaN       NaN       NaN   \n",
       "327  0.03717  0.00449  ...       NaN       NaN        NaN       NaN       NaN   \n",
       "895  0.03788  0.01061  ...      16.0      11.0       15.0     25.37     37.31   \n",
       "929  0.03987  0.00337  ...      22.0      16.0        0.0      3.75     13.75   \n",
       "\n",
       "     东侧中包波动率1  东侧中包波动率2  角部裂纹实际值  缺陷情况  检测情况  \n",
       "107       NaN       NaN      2.0   1.0   1.0  \n",
       "327       NaN       NaN      1.0   1.0   1.0  \n",
       "895     35.82     44.78      1.0   1.0   1.0  \n",
       "929      0.00      0.00      1.0   1.0   1.0  \n",
       "\n",
       "[4 rows x 114 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Data_Defect_known.iloc[0:4, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d83962ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Column 1:\n",
      "  Class 1.0: 20 samples\n",
      "  Class 2.0: 7 samples\n"
     ]
    }
   ],
   "source": [
    "# 选择要统计的多列数据\n",
    "selected_columns = Data_Defect_known.iloc[:, 111:112]\n",
    "\n",
    "# 初始化一个字典来存储每列每个类别的数量\n",
    "class_counts = {}\n",
    "\n",
    "# 针对每一列进行统计\n",
    "for col_idx in range(selected_columns.shape[1]):\n",
    "    col_data = selected_columns.iloc[:, col_idx]\n",
    "    unique_classes, counts = np.unique(col_data, return_counts=True)\n",
    "    class_counts[col_idx] = dict(zip(unique_classes, counts))\n",
    "\n",
    "# 打印每个类别在每一列中的数量\n",
    "for col_idx, counts_dict in class_counts.items():\n",
    "    print(f\"Column {col_idx + 1}:\")\n",
    "    for cls, count in counts_dict.items():\n",
    "        print(f\"  Class {cls}: {count} samples\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "335edc81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X.shape, y.shape: (27, 110) (27,) (38, 110)\n"
     ]
    }
   ],
   "source": [
    "# 生成特征与标签\n",
    "train_features = Data_Defect_known.iloc[:, 1:111]\n",
    "train_labels = Data_Defect_known.iloc[:, 111]\n",
    "test_features = Data_Defect_unknown.iloc[:, 1:111]\n",
    "print('X.shape, y.shape: {} {} {}'.format(train_features.shape, train_labels.shape, \n",
    "                                          test_features.shape))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d7901f6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all_features.shape: (65, 200)\n"
     ]
    }
   ],
   "source": [
    "# 数据转换\n",
    "all_features = pd.concat((train_features, test_features))\n",
    "# 返回类型不为object列的列名，通过将特征重新缩放到0均值和单位方差来标准化数据\n",
    "numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index\n",
    "all_features[numeric_features] = all_features[numeric_features].apply(\n",
    "                                            lambda x: (x - x.mean()) / (x.std()))\n",
    "# 在标准化数据之后，所有均值消失，因此我们可以将缺失值设置为平均值0\n",
    "all_features[numeric_features] = all_features[numeric_features].fillna(0)\n",
    "# “Dummy_na=True”将“na”（缺失值）视为有效的特征值，并为其创建指示符特征\n",
    "all_features = pd.get_dummies(all_features, dummy_na=True)\n",
    "print('all_features.shape:', all_features.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0cff36d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original shape: (65, 200)\n",
      "Reduced shape: (65, 21)\n"
     ]
    }
   ],
   "source": [
    "# PCA降维\n",
    "pca = PCA(n_components = 0.85, svd_solver = 'full')\n",
    "pca.fit(all_features)\n",
    "features_pca = pca.transform(all_features)\n",
    "print(\"Original shape: {}\".format(str(all_features.shape)))\n",
    "print(\"Reduced shape: {}\".format(str(features_pca.shape)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "16438d72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "features.shape, labels.shape: (27, 21) (27,) (38, 21)\n"
     ]
    }
   ],
   "source": [
    "# PCA后的数据集\n",
    "n_train = train_features.shape[0]\n",
    "features_train = features_pca[:n_train, :]\n",
    "labels_train = train_labels\n",
    "features_test = features_pca[n_train:, :]\n",
    "features_test_index = all_features.iloc[n_train:, :]\n",
    "print('features.shape, labels.shape: {} {} {}'\n",
    "      .format(features_train.shape, labels_train.shape, features_test.shape,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "89c7c589",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KNeighborsClassifier(n_neighbors=3)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">KNeighborsClassifier</label><div class=\"sk-toggleable__content\"><pre>KNeighborsClassifier(n_neighbors=3)</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "KNeighborsClassifier(n_neighbors=3)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 划分训练集和测试集\n",
    "X_train, y_train = features_train, labels_train\n",
    "# 初始化KNN分类器\n",
    "knn_classifier = KNeighborsClassifier(n_neighbors = 3)\n",
    "# 训练模型\n",
    "knn_classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "09711d17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "predictions.shape: (38,)\n"
     ]
    }
   ],
   "source": [
    "# 进行预测\n",
    "features_array = features_test\n",
    "predictions = knn_classifier.predict(features_array)\n",
    "print('predictions.shape:', predictions.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f3ac2aaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将预测结果转为 DataFrame，并设置相同的index\n",
    "predictions_df = pd.DataFrame(predictions, columns=['角部裂纹实际值'], index=features_test_index.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5233ccb1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data_Defect_unknown_new.shape: (38, 113)\n"
     ]
    }
   ],
   "source": [
    "# 将预测结果与特征合并\n",
    "Data_Defect_unknown_new = pd.concat([test_features, predictions_df], axis=1)\n",
    "Data_Defect_unknown_new['缺陷情况'] = 1\n",
    "Data_Defect_unknown_new['检测情况'] = 1\n",
    "print('Data_Defect_unknown_new.shape:', Data_Defect_unknown_new.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "653463f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data_Defect_known_new.shape: (27, 113)\n",
      "Data_Defect_Exist: (65, 113)\n"
     ]
    }
   ],
   "source": [
    "# 合并检测中的有缺陷的数据\n",
    "Data_Defect_known_new = pd.concat([train_features, train_labels], axis=1)\n",
    "Data_Defect_known_new['缺陷情况'] = 1\n",
    "Data_Defect_known_new['检测情况'] = 1\n",
    "print('Data_Defect_known_new.shape:', Data_Defect_known_new.shape)\n",
    "# 将预测结果添加到原始 DataFrame\n",
    "Data_Defect_Exist = pd.concat([Data_Defect_known_new, \n",
    "                                 Data_Defect_unknown_new], axis=0)\n",
    "print('Data_Defect_Exist:', Data_Defect_Exist.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7326ca2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data_noDefect: (4083, 113)\n"
     ]
    }
   ],
   "source": [
    "# 创建一个条件，判断缺陷情况等于0或1\n",
    "condition_def = Data['缺陷情况'] == 0\n",
    "Data_noDefect = Data[condition_def].iloc[:, 1:114]\n",
    "print('Data_noDefect:', Data_noDefect.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "de21a08c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data_All: (4148, 113)\n"
     ]
    }
   ],
   "source": [
    "Data_All = pd.concat([Data_Defect_Exist, Data_noDefect], axis=0)\n",
    "print('Data_All:', Data_All.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a0a6b5be",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将清理后的数据写入新的CSV文件\n",
    "output_file = '..\\Data_V3.csv'\n",
    "Data_All.to_csv(output_file, index=False, encoding='utf-8-sig')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "AI",
   "language": "python",
   "name": "ai"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
